//
//  FileXPTModel.m
//  TransPas
//
//  Created by hwly on 2022/5/17.
//

#import "FileXPTModel.h"
#import "XMLReader.h"

@interface FileXPTModel ()

@end

@implementation FileXPTModel

- (void)beginAction
{
    [self printTextFormiOSData];
//    [self createiOSString2];
//    [self printTextFormAndroidData:YES];
//    [self createAndroidXML];
}

- (void)createiOSString2
{
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ch" ofType:@"txt"];
    NSString *filePath = [self getAppFilePath:@"ch.txt"];

    NSString *stringText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

//    NSString *cbaPath = [[NSBundle mainBundle] pathForResource:@"cba" ofType:@"txt"];
    NSString *cbaPath = [self getAppFilePath:@"cba.txt"];

    NSString *cbaStringText = [NSString stringWithContentsOfFile:cbaPath encoding:NSUTF8StringEncoding error:nil];
    NSArray *cbaTextArray = [cbaStringText componentsSeparatedByString:@"\n"];

    NSArray *textArray = [stringText componentsSeparatedByString:@"\n"];

    NSMutableString *iosText = [[NSMutableString alloc] init];

    [iosText appendString:@"\n\n"];

    NSInteger i = 0;

    for (NSString *text in textArray) {
        NSArray *itemText = [text componentsSeparatedByString:@"="];
        [iosText appendString:itemText.firstObject];
        [iosText appendString:@" = \""];
        [iosText appendString:cbaTextArray[i]];
        [iosText appendString:@"\";\n"];
        i += 1;
    }

    NSLog(@"%@", iosText);
}

- (void)printTextFormiOSData
{
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ch" ofType:@"txt"];

    NSString *filePath = [self getAppFilePath:@"ch.txt"];

    NSString *stringText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    NSArray *textArray = [stringText componentsSeparatedByString:@"\n"];

    NSMutableString *iosText = [[NSMutableString alloc] init];

    [iosText appendString:@"\n\n"];

    for (NSString *text in textArray) {
        NSArray *itemText = [text componentsSeparatedByString:@"\" = \""];
        [iosText appendString:[itemText.lastObject stringByReplacingOccurrencesOfString:@"\";" withString:@""]];
        [iosText appendString:@"\n"];
    }

    NSLog(@"%@", iosText);
}

- (void)printTextFormAndroidData:(BOOL)isTest
{
    NSError *error;
    NSString *filePath = [self getAppFilePath:@"abc_tw.xml"];

//    [[NSBundle mainBundle] pathForResource:@"abc_tw" ofType:@"xml"];
    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    NSDictionary *dic = [XMLReader dictionaryForXMLData:xmlData error:&error];
    NSArray<NSDictionary *> *dicArray = dic[@"root"][@"string"];

    NSMutableString *text = [[NSMutableString alloc] init];

    [text appendString:@"\n\n"];

    for (NSDictionary *textDic in dicArray) {
        if (isTest) {
            [text appendString:textDic[@"text"]];
            [text appendString:@"\n"];
        } else {
            [text appendString:@"\""];
            [text appendString:textDic[@"name"]];
            [text appendString:@"\""];
            [text appendString:@" = "];
            [text appendString:@"\""];
            [text appendString:textDic[@"text"]];
            [text appendString:@"\";"];
            [text appendString:@"\n"];
        }
    }

    NSLog(@"%@", text);
}

- (void)createAndroidXML
{
//    NSString *cbaPath = [[NSBundle mainBundle] pathForResource:@"cba" ofType:@"txt"];

    NSString *cbaPath = [self getAppFilePath:@"cba.txt"];
    NSString *stringText = [NSString stringWithContentsOfFile:cbaPath encoding:NSUTF8StringEncoding error:nil];

    NSArray *textArray = [stringText componentsSeparatedByString:@"\n"];

    NSError *error;
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"xml"];
    NSString *filePath = [self getAppFilePath:@"abc.xml"];

    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    NSDictionary *dic = [XMLReader dictionaryForXMLData:xmlData error:&error];
    NSArray<NSDictionary *> *dicArray = dic[@"root"][@"string"];

    NSMutableString *text = [[NSMutableString alloc] init];

    [text appendString:@"\n\n"];

    NSUInteger index = 0;

    if (textArray.count >= dic.count) {
        for (NSDictionary *textDic in dicArray) {
            [text appendString:@"<string name=\""];
            [text appendString:textDic[@"name"]];
            [text appendString:@"\">"];
            [text appendString:textArray[index]];
            [text appendString:@"</string>\n"];
            index += 1;
        }
    } else {
        NSLog(@"导出文件错误");
    }

    NSLog(@"%@", text);
}

- (void)countryPlist
{
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"countryCh" ofType:@"plist"];

    NSString *filePath = [self getAppFilePath:@"countryCh.plist"];

    NSMutableDictionary *countryDic = [[NSDictionary dictionaryWithContentsOfFile:filePath] mutableCopy];

    NSArray *keyArray = [countryDic.allKeys sortedArrayUsingSelector:@selector(compare:)];

    NSMutableString *text = [[NSMutableString alloc] init];

    [text appendString:@"\n\n"];
    [text appendString:@"[\n"];

    NSString *twPath = [self getAppFilePath:@"Country_TW.txt"];
//    NSString *twPath = [[NSBundle mainBundle] pathForResource:@"Country_TW" ofType:@"txt"];
    NSString *twText = [NSString stringWithContentsOfFile:twPath encoding:NSUTF8StringEncoding error:nil];
    NSArray *twArray = [twText componentsSeparatedByString:@"\n"];

    NSUInteger index = 0;

    for (NSString *key in keyArray) {
        NSArray *array = [countryDic objectForKey:key];

        for (NSString *ctext in array) {
            NSArray *carray = [ctext componentsSeparatedByString:@"+"];
            [text appendString:@"{\n"];
            [text appendString:@"\"letter\": \""];
            [text appendString:key];
            [text appendString:@"\",\n"];
            [text appendString:@"\"country\": \""];
            [text appendString:twArray[index]];
            [text appendString:@"\",\n"];
            [text appendString:@"\"code\": \"+"];
            [text appendString:carray.lastObject];
            [text appendString:@"\"},\n"];

//            [text appendString:carray.firstObject];
//            [text appendString:@"-"];
//            [text appendString:twArray[index]];
//            [text appendString:@"\n"];
            index += 1;
        }
    }

    [text appendString:@"]\n"];

    NSLog(@"%@", text);
}

- (NSString *)getAppFilePath:(NSString *)fileName
{
    return [NSString stringWithFormat:@"/Users/hwly/Desktop/TransPas/TransPas/%@", fileName];
}

@end
